package com.syj.jsxjj.ui.renzheng.activity;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.syj.jsxjj.R;
import com.syj.jsxjj.app.App;
import com.syj.jsxjj.base.BaseActivity;
import com.syj.jsxjj.http.HttpManager;
import com.syj.jsxjj.http.HttpSubscriber;
import com.syj.jsxjj.ui.renzheng.bean.ContentBean;
import com.syj.jsxjj.ui.renzheng.bean.PersonalBean;
import com.syj.jsxjj.ui.renzheng.fragment.AuthenFragment;
import com.syj.jsxjj.util.FormatUtil;
import com.syj.jsxjj.util.StringUtil;
import com.syj.jsxjj.util.ToastUtil;
import com.syj.jsxjj.widget.ClearEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 个人信息
 */
public class AuthenPersonalInformationActivity extends BaseActivity {

    @BindView(R.id.et_card_name)//姓名
            ClearEditText mEtCardName;
    @BindView(R.id.et_card_number)
    ClearEditText mEtCardNumber;//身份证
    @BindView(R.id.layout_choose_degree)
    LinearLayout mLayoutChooseDegree;
    @BindView(R.id.tv_degree)
    TextView mTvDegree;//学历
    private OptionsPickerView pv;
    private ArrayList<String> professionalList = new ArrayList<>();
    private ContentBean contentBean;
    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_information;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void loadData() {
        mActivity = AuthenPersonalInformationActivity.this;
        contentBean = getIntent().getParcelableExtra(AuthenFragment.CONTENT_BEAN);
        if (contentBean==null){
            contentBean = new ContentBean();
        }
        if (contentBean.getPersonal()!=null){
            mEtCardName.setText(contentBean.getPersonal().getName());
            mEtCardNumber.setText(contentBean.getPersonal().getId_card());
            mTvDegree.setText(contentBean.getPersonal().getWork());
        }
        initView();
        setData();
    }

    private void initView() {
        mTitle.setTitle(true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, R.string.personal_information);
        mTitle.setRightTitle("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }



    @OnClick({ R.id.layout_choose_degree})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_choose_degree:
                if (pv == null) {
                    professionalList.add("上班族");
                    professionalList.add("自由职业");
                    professionalList.add("学生");
                    pv = new OptionsPickerView(mContext);
                    pv.setPicker(professionalList);
                    pv.setCyclic(false);
                    pv.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int option2, int options3) {
                            mTvDegree.setText(professionalList.get(options1));
                        }
                    });
                }
                pv.show();
                break;
        }
    }

    //设置数据
    private void setData() {
        //获取个人信息请求数据
    }

    /**
     * 个人信息保存
     **/
    private void save() {
        if (check()) {
            PersonalBean personalBean = new PersonalBean();
            personalBean.setName(mEtCardName.getText().toString());
            personalBean.setId_card(mEtCardNumber.getText().toString());
            personalBean.setWork(mTvDegree.getText().toString());
            contentBean.setPersonal(personalBean);
            toSubscribe(HttpManager.getApi().saveCertification("1",new Gson().toJson(contentBean)), new HttpSubscriber() {
                @Override
                protected void _onStart() {
                    App.loadingDefault(mActivity);
                }

                @Override
                protected void _onNext(Object o) {
                    ToastUtil.showToast("保存成功");
                    finish();
                }

                @Override
                protected void _onError(String message) {
                    ToastUtil.showToast(message);
                }

                @Override
                protected void _onCompleted() {
                    App.hideLoading();
                }
            });
        }
    }

    private boolean check() {
       if (StringUtil.isBlankEdit(mEtCardName)) {
            ToastUtil.showToast("请输入真实姓名");
        } else if (StringUtil.isBlankEdit(mEtCardNumber)|| !FormatUtil.isIdCardNo(mEtCardNumber.getText().toString())) {
            ToastUtil.showToast("请输入正确的身份证号");
        } else if (StringUtil.isBlankEdit(mTvDegree)) {
            ToastUtil.showToast("请选择职业类型");
        } else {
            return true;
        }
        return false;
    }

}
